package cn.yudao.org.controller;

import cn.yudao.basic.query.AjaxResult;
import cn.yudao.basic.utils.PageList;
import cn.yudao.org.domain.Department;
import cn.yudao.org.query.DepartmentQuery;
import cn.yudao.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService service;

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/24 18:03  
    * @Parameters: 
    * @Return 
    */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> loadAll(){
        return  service.findAllDepartment();
    }

    /**
    * @Title: loadById
    * @Description: 根据id查询
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/24 18:03
    * @Parameters:
    * @Return
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department loadById(@ApiParam(name="ID属性",required = true) @PathVariable Long id){
        return service.findByIdDepartment(id);
    }

    /**
    * @Title: remove
    * @Description: 通过id删除
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/24 18:17
    * @Parameters:
    * @Return
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@ApiParam(name="ID属性",required = true) @PathVariable Long id){
        try {
            service.removeDepartment(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }


    /**
    * @Title: addOrUpt
    * @Description: 新增或者修改
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/24 18:20
    * @Parameters:
    * @Return
    */
    @PutMapping
    @ApiOperation(value = "新增或者修改" )
    public AjaxResult addOrUpt(@ApiParam(name="部门",required = true) @RequestBody Department department){
        try {
            if (department.getId() == null){
                service.addDepartment(department);
            }else {
                service.uptDepartment(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统异常，请等会再试");
        }
    }

    /**
    * @Title: queryDept
    * @Description: 高级查询和分页查询
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/24 18:27
    * @Parameters:
    * @Return
    */
    @PostMapping
    @ApiOperation(value = "高级查询和分页" )
    public PageList queryDept(@ApiParam(name="高级查询字段和分页",required = true) @RequestBody DepartmentQuery departmentQuery){
        System.out.println(departmentQuery.getKeyWork());
        PageList pageList = service.queryDepartment(departmentQuery);
        return pageList;
    }


    
    

}
